package com.ihealthway.all.groupbuy.modules.front;

import com.ihealthway.all.groupbuy.services.RewardInfoService;
import com.ihealthway.all.groupbuy.services.WinnerLogService;
import com.ihealthway.all.groupbuy.vo.Reward;
import com.ihealthway.all.groupbuy.vo.WinnerLog;
import com.ihealthway.all.commons.Constants;
import com.ihealthway.all.utils.Ajax;
import com.ihealthway.all.utils.AjaxReturn;
import com.ihealthway.all.groupbuy.vo.UserInfo;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.POST;
import org.nutz.mvc.annotation.Param;

import javax.servlet.http.HttpServletRequest;

/**
 * All lottery draw enter module
 * @author 凡梦星尘(elkan1788@gmail.com)
 */
@At("/front/lotdraw")
@IocBean
public class LotteryDrawModule {

    private static final Log logger = Logs.get();

    @Inject
    private RewardInfoService rwdInfoService;
    @Inject
    private WinnerLogService winLogService;

    @At
    @Ok("beetl:front/scratch_card.jsp")
    public void scratchCard(HttpServletRequest req) {
        UserInfo userInfo = (UserInfo) req.getSession().getAttribute(Constants.WECHAT_LOGIN_SESSION);
        boolean flag = winLogService.checkWinLog(userInfo.getUserId());
        int rwdId = 0;
        String rwdName = "Not Win";
        String rwdImg = "";
        int winId = 0;
        if(!flag) {
            Reward reward = rwdInfoService.winLottery();
            WinnerLog wl = new WinnerLog();
            wl.setUserId(userInfo.getUserId());
            wl.setShareCount(0);
            wl.setDataStatus("0");
            if (reward!=null) {
                rwdId = reward.getRewardId();
                rwdName = reward.getRewardName();
                rwdImg = reward.getRewardImg();
                wl.setRewardId(reward.getRewardId());
                wl.setDataStatus("1");
            }
            WinnerLog tmp = winLogService.addWinnerLog(wl);
            winId = tmp.getWinId();
        } else {
            rwdId = -1;
            winId = -1;
            logger.infof("User[%d-%s] had win lottery in past!", userInfo.getUserId(), userInfo.getOpenId());
        }

        req.setAttribute("rwdId", rwdId);
        req.setAttribute("rwdName", rwdName);
        req.setAttribute("rwdImg", rwdImg);
        req.setAttribute("winId", winId);
    }

    @At
    @POST
    public AjaxReturn bookInWinLog(@Param("winId") Integer winId, @Param("wUser") String wUser,
                                   @Param("wPhone") String wPhone, @Param("wAreas") String wArea,
                                   @Param("wAddr") String wAddr, HttpServletRequest req) {
        AjaxReturn ar = Ajax.ok();
        UserInfo userInfo = (UserInfo) req.getSession().getAttribute(Constants.WECHAT_LOGIN_SESSION);
        WinnerLog wl = new WinnerLog();
        wl.setWinId(winId);
        wl.setUserId(userInfo.getUserId());
        wl.setWinnerPhone(wPhone);
        wl.setWinnerName(wUser);
        String[] areas = wArea.split(" ");
        wl.setWinnerProvince(areas[0]);
        wl.setWinnerCity(areas[1]);
        wl.setWinnerArea(areas[2]);
        wl.setWinnerAddress(wAddr);
        boolean updated = winLogService.updateWinnerLog(wl);
        ar.setOk(updated);
        return ar;
    }
}
